package net.cloudcodex.client.character.presenter;

import java.util.List;

import net.cloudcodex.client.CloudCodexEventBus;
import net.cloudcodex.client.cdo.CharacterCDO;
import net.cloudcodex.client.character.detail.presenter.CharacterDetailPresenter;
import net.cloudcodex.client.character.view.CharacterView;
import net.cloudcodex.client.character.view.CharacterViewImpl;
import net.cloudcodex.client.character.view.CharactersView.Message;
import net.cloudcodex.client.storyboard.player.presenter.PlayerBoardPresenter;
import net.cloudcodex.client.util.Container;
import net.cloudcodex.client.util.rpc.SuccessCallback;
import net.cloudcodex.shared.dto.NotificationDTO;
import net.cloudcodex.shared.dto.Result;
import net.cloudcodex.shared.dto.campaign.character.CharacterHeaderDTO;
import net.cloudcodex.shared.dto.campaign.character.CompleteCharacterDescriptionDTO;
import net.cloudcodex.shared.dto.home.CharacterSummaryDTO;
import net.cloudcodex.shared.rpc.CampaignRPCAsync;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

/**
 * 
 * 
 * @author ADaroussin
 */
@Presenter(view = CharacterViewImpl.class, multiple = true)
public class CharacterPresenter extends BasePresenter<CharacterView, CloudCodexEventBus> {
	private final Message message;

	private CampaignRPCAsync campaignService;
	private CharacterSummaryDTO characterSummaryDTO;
	protected String selectedContent;
	private boolean isInit = false;

	protected List<CharacterHeaderDTO> characterHeaderDTOList;

	private String nickName;

	private long userId;

	@Inject
	public CharacterPresenter(Message pMessage, CampaignRPCAsync campaignService) {
		this.message = pMessage;
		this.campaignService = campaignService;
	}

	@Override
	public void bind() {
		this.view.getNotificationListBox().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// ListBox listBox = (ListBox) event.getSource();
				// String text =
				// listBox.getItemText(listBox.getSelectedIndex());
				// System.out.println("test = " + text);

			}
		});

		this.view.getStoryBoardButton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				final Container<PlayerBoardPresenter> container = new Container<PlayerBoardPresenter>();
				eventBus.newPlayerBoard(characterSummaryDTO.getCampaign().getId(), characterSummaryDTO.getId(),
						container);
				view.refreshContent(container.get().getView());
			}
		});
		this.view.getCharacterDescriptionButton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				campaignService.getCharacterCompleteDescription(characterSummaryDTO.getCampaign().getId(),
						characterSummaryDTO.getId(), new SuccessCallback<Result<CompleteCharacterDescriptionDTO>>() {

							@Override
							public void onSuccess(Result<CompleteCharacterDescriptionDTO> result) {
								CompleteCharacterDescriptionDTO completeCharacterDescriptionDTO = result.getData();
								CharacterCDO characterCDO = new CharacterCDO(completeCharacterDescriptionDTO);
								CharacterDescriptionPresenter characterDescriptionPresenter = eventBus
										.addHandler(CharacterDescriptionPresenter.class);
								characterDescriptionPresenter.setByCharacterId(characterSummaryDTO.getId());
								characterDescriptionPresenter.initCharacterDescriptionPanel(characterCDO);
								view.refreshContent(characterDescriptionPresenter.getView());
							}
						});
			}
		});
		this.view.getAltCharacterDescriptionButton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				final Container<CharacterDetailPresenter> container = new Container<CharacterDetailPresenter>();
				eventBus.newCharacterDetail(characterSummaryDTO.getId(), characterSummaryDTO, container);
				view.refreshContent(container.get().getView());
			}
		});
	}

	public void setSelectedContent(String selectedContent) {
		this.selectedContent = selectedContent;
	}

	public void addCharacterSummary(CharacterSummaryDTO characterSummaryDTO) {
		this.characterSummaryDTO = characterSummaryDTO;

	}

	public void initView() {
		initNotificationPanel();
		isInit = true;
	}

	public void display() {
		eventBus.addCharacterTab(view, characterSummaryDTO.getName(), characterSummaryDTO.getId());

	}

	private void initNotificationPanel() {
		List<NotificationDTO> characterNotificationDTOs = characterSummaryDTO.getNotifications();
		if (characterNotificationDTOs != null && !characterNotificationDTOs.isEmpty()) {
			for (NotificationDTO characterNotificationDTO : characterNotificationDTOs) {
				this.view.addNotification(characterNotificationDTO.getMessage(), characterNotificationDTO.getDate());

			}
		}
		List<NotificationDTO> campaignNotificationDTOs = characterSummaryDTO.getCampaign().getNotifications();
		if (campaignNotificationDTOs != null && !campaignNotificationDTOs.isEmpty()) {
			for (NotificationDTO campaignNotificationDTO : campaignNotificationDTOs) {

				this.view.addNotification(campaignNotificationDTO.getMessage(), campaignNotificationDTO.getDate());
			}
		}
	}

	public void onSetNickName(String nickName) {
		this.nickName = nickName;
	}

	public boolean isViewInitialized() {
		return isInit;
	}

}
